package cn.com.mdx.linkedList;

/**
 * <p>
 *
 * @author ：huangy
 * @date ：Created in 2020/11/5 8:42 下午
 */
public class LinkedList<T> {

    /**
     * 头指针
     */
    private Node<T> head;

    /**
     * 尾指针
     */
    private Node<T> tail;

    private static class Node<T> {
        /**
         * 存放的数据
         */
        private final T t;
        private Node<T> next;

        public Node(T t, Node<T> next) {
            this.t = t;
            this.next = next;
        }
    }

    public void insert(T t) {
        if (head == null) {
            head = new Node<>(t, null);
            tail = head;
        } else {
            Node<T> temp = new Node<>(t, null);
            tail.next = temp;
            tail = temp;
            tail.next = head;
        }
    }

    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        int N = 10;
        int callNumber = 5;
        for (int i = 0; i < N; i++) {
            linkedList.insert(i + 1);
        }
        int flag = 0;
        Node<Integer> test = linkedList.head;
        while (flag <= 12) {
            System.out.println(test.t);
            test = test.next;
            flag++;
        }
        System.out.println("---------------");
        flag = 0;
        int endFlag = 1;
        Node<Integer> temp1 = linkedList.head;
        Node<Integer> temp2 = temp1;
        while (endFlag <= N) {
            if (++flag == callNumber) {
                flag = 0;
                System.out.println(temp2.t);
                temp1.next = temp2.next;
                endFlag++;
            }
            temp1 = temp2;
            temp2 = temp2.next;
        }
    }

}
